/*
In case it matters, this code is hereby placed under the MIT/X11 License:

Copyright (c) 2011, Kevin A. Wortman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <fstream>
#include <sstream>  // PackageSet::show uses a stringstream.

#include "deb_knap.h"

using namespace std;

PackageVector* loadPackages(const std::string& path) {
  ifstream f(path.c_str());

  if (!f)
    return NULL;

  PackageVector* result = new PackageVector();

  // Number of packages.
  int n;
  f >> n;

  // Read each package.
  for (int i = 0; i < n; ++i) {
    string name;
    int votes, size;
    f >> name >> votes >> size;
    result->push_back(Package(name, votes, size));
  }

  if (!f) {
    // f is in the fail state, so an I/O error happened at some point.
    delete result;
    f.close();
    return NULL;
  } else {
    // Success.
    f.close();
    return result;
  }
}

int PackageSet::totalVotes() const {
  int result = 0;
  // Here we behave as good STL citizens and use an iterator object
  // instead of an int counter.
  for (vector<int>::const_iterator i = _indices.begin();
       i != _indices.end();
       ++i)
    result += _allPackages[*i].votes();
  return result;
}

int PackageSet::totalSize() const {
  int result = 0;
  for (vector<int>::const_iterator i = _indices.begin();
       i != _indices.end();
       ++i)
    result += _allPackages[*i].size();
  return result;
}

void PackageSet::insert(int index) {
  assert(index >= 0);
  assert(index < _allPackages.size());
  _indices.push_back(index);
}

std::string PackageSet::show(int indent) const {
  // Build the return value using an STL stringstream.
  assert(indent >= 0);
  string indentSpaces(indent, ' ');
  stringstream f;
  for (vector<int>::const_iterator i = _indices.begin();
       i != _indices.end();
       ++i) {
    const Package& p = _allPackages[*i];
    f << indentSpaces << p.name() << ' ' << p.size() 
      << ' ' << p.votes() << endl;
  }
  f << indentSpaces << "total size=" << totalSize()
    << " total votes=" << totalVotes() << endl;
  return f.str();
}
